// register.js
var util = require('../../utils/util.js');
var validate = require('../../utils/validate.js');
var cart = require('../../utils/cart.js');
var commonapi = require('../../component/service/commonapi.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    returnType:false,
    mobile: "",
    password: "",
    smsCapatch: "",
    userName: "",
    authButton:false,
    verifyCode: "发送验证码",
    tokenKey:''
  },

  onLoad: function (options) {
    let returnType = false;
    if(typeof(options.returnType)!="undefined"&&(options.returnType)){
      returnType = true;
    }
    this.setData({
      returnType:returnType,
      tokenKey:options.tokenKey
    })
  },
  changeCode: function () {

  },

  inputMobile: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },

  inputPswd: function (e) {
    this.setData({
      password: e.detail.value
    })
  },

  inputUserName: function(e){
    this.setData({
      userName: e.detail.value
    })
  },
  inputSmsCaptcha: function(e){
    this.setData({
      smsCaptcha: e.detail.value
    })
  },
  sendSMS: function(e){
    if (!validate.validateMobile(this.data.mobile))
      return;
    util.auth_mobile(this, this.data.mobile);
  },
  //用户一键注册
  getPhoneNumber: function(e) {
    if(commonapi.verifySDKVersion(1.2)&&e.detail.errMsg == 'getPhoneNumber:ok'){
      commonapi.onekeyRegister(e.detail.iv,e.detail.encryptedData,this.data.returnType,2,this.data.tokenKey);
    }
  },

  register: function (e) {
    var mobile = this.data.mobile;
    var password = this.data.password;
    var smsCaptcha = this.data.smsCaptcha;
    var userName = this.data.userName;

    if (userName.length == 0) {
      wx.showToast({
        title: '请输入姓名',
      })
      return;
    }

    if (password.length == 0) {
      wx.showToast({
        title: '请输入密码',
      })
      return;
    }
    //校验手机号
    if (!validate.validateMobile(mobile))
      return;
    
    if (smsCaptcha.length == 0) {
      wx.showToast({
        title: '请输入验证码',
      })
      return;
    }
    //注册并绑定微信用户
    let that = this;
    wx.login({
      success: function (resp) {
        wx.request({
          data: { mobile: mobile, password: password, repeatPassword: password, wechatCode:resp.code, userName:userName, smsCaptcha:smsCaptcha },
          url: getApp().globalData.host + "/webapp/control/registerWechat.html",
          header: getApp().globalData.header,
          success: function (res) {
            if (res.data.success) {
              if (res.data.success) {
                getApp().globalData.header.cookie = "YNITTC_SOURCE=WECHAT;YNITTC_SESSION_ID=" + res.data.token;
                getApp().globalData.platUser = res.data.user;
                wx.showToast({
                  title: "注册成功",
                })
                cart.register();
                if(that.data.returnType){//跳转回去
                  wx.navigateBack({delta: 2})
                }else{
                  //返回首页
                  wx.switchTab({
                    url: '/pages/index/index'
                  })
                }
              }
            } else {
              wx.showToast({
                title: res.data.errorMsg,
              })
            }
          }
        })
      }
    })
  }
  
})
